from lxml import etree
import requests
import pandas as pd
url="http://www.imooc.com/course/list"
params={
    "user-agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36 Edg/111.0.1661.62"
}
responese=requests.get(url,params=params)
responese.encoding="utf-8"
html=etree.HTML(responese.text)
# def parser_content(html):
#     #转化成xpath解析的格式
#     selector = etree.HTML(html)
#     #获取每个课程的信息
#     courseDetail = selector.xpath('//*[@id="main"]/div[3]/div[1]/a')
#     courseinfo=[]
#     for courseDetail in courseDetail:
#         name = courseDetail.xpath('.//p[@class="title ellipsis2"]/text()')[0]
#         student=courseDetail.xpath('.//p[@class="one"]/text()')[0]
#         img_url='http:' + courseDetail.xpath('.//div[@class="img"]/@style')[0]
#         url = "http://www.imooc.com"+courseDetail.xpath('.//@href')[0]
#         courseinfo.append((name,student,img_url,url))
#     return courseinfo
# parser_content(html)
# def save_csv(courseinfo):
#     DataFrame = pd.DataFrame(courseinfo)
#     DataFrame.to_csv("mooc.csv",header=)
a_lists=html.xpath('//*[@id="main"]/div[3]/div[1]/a')
courseinfo=[]
for i in a_lists:
  book_name=i.xpath('.//p[1]/text()')[0]
  book_url=i.xpath('.//@href')[0]
  image_url = i.xpath('.//div/@style')[0]
  book_number= i.xpath('.//p[2]/text()')[0]
  print("书名:",book_name)
  print("url:",book_url)
  print("图片url",image_url)
  print("人数",book_number)
  courseinfo.append((book_name,book_url,image_url,book_number))
print(courseinfo)
#存储到csv文件中
DataFram = pd.DataFrame(courseinfo)
DataFram.to_csv("1.csv")






